package so.ifl.manager.welcome;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.Constant;
import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.MD5;
import so.ifl.commons.tools.StringHelper;
import so.ifl.servcie.AccountService;
import so.ifl.servcie.ReportService;
import so.ifl.vo.AccountVo;

/**
 *create by aaron.lu
 *create at 2012-1-7 下午3:26:58
 *<mailto: atyuan.lu@gmail.com>
 **/
@Component("welcomeManager")
public class WelcomeManager {
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private ReportService reportService;
	
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	@Expose
	public Map<String, Object> login(String accountLoginName, String accountPwd){
		Map<String, Object> result = new HashMap<String, Object>();
		
		String successFlag = Constant.SUCCESS_FALSE_FLAG;		
		Map<String, Object> params = new HashMap<String, Object>();
		
		if (StringHelper.isNotEmpty(accountLoginName) && StringHelper.isNotEmpty(accountPwd)) {
			
			accountService.firstInitApp(accountLoginName, accountPwd);
			
			
			params.put("account_login_name_", accountLoginName);
			params.put("account_pwd_", MD5.getMD5(accountPwd));
			List<AccountVo> list = accountService.queryLoginAccount(params);			
			
			if (null != list && list.size() > 0) {				
				logger_.debug(list.get(0) + "login...");
				
				DoradoSystemUtils.putCurrentUser(DoradoContext.getCurrent().getRequest(), list.get(0));//设置当前用户
				successFlag = Constant.SUCCESS_TRUE_FLAG;
				result.put("current_user", list.get(0));
			}
		}
		
		result.put("flag", successFlag);
		
		
		return result;
		
	}
	
	@DataProvider
	public Collection<Map<String, Object>> getDataReport(Map<String, Object> params) {
		Integer officeId = null;
		
		if (null != params) {
			officeId = (Integer)params.get("office_id_");
		}
		
		List<Map<String, Object>> list = reportService.countStudent(officeId);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		if (null != list && list.size() > 0) {
			Map<String, Object> re = list.get(0);
			Map<String, Object> record1 = new HashMap<String, Object>();
			Map<String, Object> record2 = new HashMap<String, Object>();
			Map<String, Object> record3 = new HashMap<String, Object>();
			Map<String, Object> record4 = new HashMap<String, Object>();      
			Map<String, Object> record5 = new HashMap<String, Object>();
			Map<String, Object> record6 = new HashMap<String, Object>();
			Map<String, Object> record7 = new HashMap<String, Object>();
			
			Integer type1 = (Integer) re.get("type1");
			Integer type2 = (Integer) re.get("type2");
			Integer type3 = (Integer) re.get("type3");
			Integer type4 = (Integer) re.get("type4");
			Integer type5 = (Integer) re.get("type5");
			Integer type6 = (Integer) re.get("type6");
			Integer type7 = (Integer) re.get("type7");
			Integer type8 = (Integer) re.get("type8");
			Integer type9 = (Integer) re.get("type9");
			Integer type10 = (Integer) re.get("type10");
			
			record7.put("value", type1);
			//result.add(record7);
			record1.put("value", type1 + type3);
			result.add(record1);
			record2.put("value", type2 + type5);
			result.add(record2);
			record3.put("value", type4 + type7);
			result.add(record3);
			record4.put("value", type6);
			result.add(record4);
			record5.put("value", type8);
			result.add(record5);
			record6.put("value", type9 + type10);
			result.add(record6);
		}
		
		logger_.debug(result.toString());
		return result;
	}
	
	@Expose
	public Map<String, Object> getDataWithOffice(Integer officeId) {
		Map<String, Object> result = new HashMap<String, Object>();
		Integer sum = reportService.countStudentByOffice(officeId);
		result.put("value", sum);
		return result;
	}
	
}
